const router = require("koa-router")();
const User = require("../model/user");
const util = require("../util/index");
router.prefix("/users");

router.get("/", function (ctx, next) {
  ctx.body = "this is a users response!";
});

router.get("/bar", function (ctx, next) {
  ctx.body = "this is a users/bar response";
});

router.post("/create", async (ctx) => {
  const { userName, password } = ctx.request.body;
  try {
    await User.create({
      userName,
      password,
    });
    ctx.body = util.success("", "创建成功", util.code.success);
  } catch (error) {
    ctx.body = util.fail(error, "创建失败", util.code.error);
  }
});

module.exports = router;
